﻿using System;
using System.Text;

namespace GPS.GPSLanguage
{
    class TimeToken
    {
        internal static void ParseLanguage(GPSData gpsData, System.Collections.Queue q)
        {
            String token = (string)q.Dequeue();

            TimeSpan ts = new TimeSpan(0, 0, 0);

            try
            {
                if (token.Length >= 6)
                {
                    int hours, minutes, seconds, millis;
                    hours = int.Parse(token.Substring(0, 2));
                    minutes = int.Parse(token.Substring(2, 2));
                    seconds = int.Parse(token.Substring(4, 2));
                    millis = 0;
                    if (token.Length > 6 && token.IndexOf('.') != -1)
                    {
                        try
                        {
                            double val = double.Parse("0" + token.Substring(6)) * 1000;
                            millis = (int)val;
                        }
                        catch { }
                    }
                    ts = new TimeSpan(0, hours, minutes, seconds, millis);
                }
            }
            catch
            {

            }

            gpsData.SetTimeTaken(ts);

            switch (gpsData.MessageType)
            {
                case MessageType.GPGGA:
                    LatitudeToken.ParseLanguage(gpsData, q);
                    break;
                case MessageType.GPRMC:
                    StatusToken.ParseLanguage(gpsData, q);
                    break;
                case MessageType.GPGLL:
                    StatusToken.ParseLanguage(gpsData, q);
                    break;
                default:
                    throw new InvalidLanguageException("How did you get here?");
            }
        }
    }
}
